﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using text.Utils;

namespace text
{
    public partial class user : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitNote();
            }
        }
     
        public void InitNote()
        {
            DataRow loginUser = (DataRow)Session["User"];
            DataSet ds = DbHelper.Query("select * from  users ");
            rptNote.DataSource = ds;
            rptNote.DataBind();
        }


        protected void rptNote_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            //获取当前行的发布人
            int userId = Convert.ToInt32((e.Item.FindControl("hfUserId") as HiddenField).Value);
            DataRow loginUser = (DataRow)Session["User"];
            //获取当前登录人权限
            int loginUserManager = Convert.ToInt32(loginUser["manager"]);
            int Id = Convert.ToInt32(loginUser["id"]);
            //判断登录人是不是日志发布人或者管理员
            //是:不处理
            //不是:删除隐藏
            if (loginUserManager != 1 )
            {
                e.Item.FindControl("LinkButton1").Visible = false;
                e.Item.FindControl("cbxItem").Visible = false;
            }
            //if (Id!=userId)
            //{
            //    e.Item.FindControl("LinkButton2").Visible = false;
            //}
        }


        protected void Button1_Click(object sender, EventArgs e)
        {

            int sign = 0;
            //遍历所有的数据行
            for (int i = 0; i < rptNote.Items.Count; i++)
            {
                //获取每一行的勾选框
                CheckBox cbx = rptNote.Items[i].FindControl("cbxItem") as CheckBox;
                int noteId = Convert.ToInt32((rptNote.Items[i].FindControl("hfNoteId") as HiddenField).Value);
                //判断勾选框是否勾选
                if (cbx.Checked == true)
                {
                    int flag = DbHelper.Execute("delete from users where id=" + noteId);
                    if (flag > 0)
                    {
                        sign++;
                    }
                }
            }
            if (sign > 0)
                Response.Write($"<script>alert('删除成功，共删除{sign}条')</script>");
            InitNote();

        }

        protected void rptNote_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {

                int id = Convert.ToInt32(e.CommandArgument.ToString());

                int flag = DbHelper.Execute(string.Format("delete from users where id={0}", id));

                if (flag > 0)
                {
                    Response.Write("<script>alert('删除成功')</script>");
                    InitNote();
                }
            }
            else
            {
                //跳页
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                Response.Redirect("info.aspx?id="+id);
            }

        }
    }
}